/**
 * 服务预约维护 - 派工页面
 * @author miaoyl
 * @date 2019/04/09
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            isDetail: false,
            form: {
                GWAK001: null,
                GWAKL004: null,
                MMRD013S: null,
                operation: null,
                MMRF007: null,
                MMRF008: null
            },
            templateImageUrl:[],
            dialogImageUrl: '',
            dialogVisible: false,
            imgStr_:{imgStr:''},
            img_list:[],
            imgs: g_ctxPath + 'upload/imgs',
            rules: {
                MMRD001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD019: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD020: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRDDOCNO:[
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD023:[
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD022:[
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD024:[
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRD025:[
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRF007:[
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRF008:[
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
                mmrd001Options:app.datas_("crm_gwai740_mmrd001"),
                mmrd005Options:app.datas_("crm_gwai740_mmrd005"),
                mmrd019Options:app.datas_("crm_gwai740_mmrd019"),
                mmrd020Options:app.datas_("crm_gwai740_mmrd020"),
                mmrd023Options:app.datas_("crm_gwai740_mmrd023"),
                mmrd013Options:[],
                mmrf007Options:[]
            },
        },
        callObj
    ;

    var Gwai740Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai740_paigong',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }
                ]
            }, win);
        },

        editData: function () {
            app.ajax({
                xurl: 'crm/gwai740/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: "服务派工成功",
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwai740').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        _call: function (tagData) {
            vueData.templateImageUrl = [];
            callObj = tagData.obj;
            app.extendUpLow({
                target: vueData.form,
                res: callObj,
                reset: true
            });
            if(tagData.is_detail){
                vueData.isDetail = true;
                app.ajax({
                    xurl: 'crm/gwai740/getMMRF',
                    xdata: {MMRF001:callObj.MMRDDOCNO,MMRF002:callObj.MMRDSEQ},
                    cb: function (result) {
                        if (result.ok) {
                            vueData.form.MMRF007 = result.data.data.MMRF007;
                            vueData.form.MMRF008 = result.data.data.MMRF008;
                        }
                    }
                });
            }else {
                vueData.isDetail = false;
            }
            if(callObj.MMRD015 != undefined && callObj.MMRD015.length > 0) {
                var list_mmrs015 = callObj.MMRD015.split(',');
                list_mmrs015.forEach(function (v) {
                    vueData.templateImageUrl.push('/wissun-smfile/' + v);
                });
            }
            if(callObj.MMRD013 != undefined && callObj.MMRD013 != ''){
                vueData.form.MMRD013S = callObj.MMRD013.split(',');
            }else {
                vueData.form.MMRD013S = [];
            }
            self.getMmrd013(callObj.MMRD013);
            app.ajax({
                xurl: 'crm/gwai740/getUsers',
                xdata: {ID: callObj.MMRF007, MMRD006: callObj.MMRD006, MMRD007: callObj.MMRD007},
                loading: true,
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.mmrf007Options = result.data.data;
                    }
                }
            });
        },

        getMmrd013:function (MMRD002) {
            app.ajax({
                xurl: 'crm/gwai740/getMmrd013List',
                xdata: {MMRD002: MMRD002},
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.mmrd013Options = result.data.data;
                    }
                }
            });
        },

        _ready: function () {
            vueData.form = app.fields_(['MMRD_T'],vueData.form);
            var vue = new Vue({
                el: '#gwai740_paigong',
                data: vueData,
                methods: {
                    cancel: function () {
                        self._closeTpl();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                vueData.form.operation = 'paigong';
                                self.editData();
                            }
                        });
                    },
                    openMmrd:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    columnList: [
                                        {label: '预约服务单号', prop: 'MMRDDOCNO' , width: '150'},
                                        {label: '单据日期', prop: 'MMRDDOCDATE', width: '150'},
                                        {label: '任务类型', prop: 'MMRD001_NAME', width: '150'},
                                        {label: '设备编号', prop: 'MMRD002', width: '150'},
                                        {label: '名称', prop: 'MMRA004', width: '150'},
                                        {label: '规格', prop: 'MMRA006', width: '150'},
                                        {label: '序列号', prop: 'MMRD003', width: '150'}
                                    ],
                                    url: 'crm/gwai740/getMmrddocnoPage',
                                    searchParam:{
                                        MMRD020:'2',//派工
                                    }
                                },
                                cb: function (data) {
                                    vueData.templateImageUrl = [];
                                    self.getMmrd013(data.MMRD002);
                                    app.extendUpLow({
                                        target: vueData.form,
                                        res: [data,{MMRD013S:data.MMRD013.split(',')}],
                                        reset: true
                                    });
                                    if(data.MMRD015 != undefined && data.MMRD015.length > 0) {
                                        var list_mmrs015 = data.MMRD015.split(',');
                                        list_mmrs015.forEach(function (v) {
                                            vueData.templateImageUrl.push('/wissun-smfile/' + v);
                                        });
                                    }
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择服务预约单',
                                area: ['1000px', '450px']
                            }
                        });
                    }

                },
                watch: {},
                created: function () {
                    self._setEleIsReady('gwai740_paigong');
                }
            });
        },

        _lazy: function () {
            this._super();
        }
    });

    return new Gwai740Edit();

});